/**
 * Copyright (c) 2020 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;


import com.example.dto.GenTableFieldDTO;
import com.example.dto.GenTableInfoDTO;
import com.example.dto.SysDictTypeDTO;
import com.example.feign.DictFeignClient;
import com.example.model.page.PageData;
import com.example.model.api.Result;
import com.example.config.DataSourceInfo;
import com.example.entity.MenuEntity;
import com.example.entity.TableFieldEntity;
import com.example.entity.TableInfoEntity;
import com.example.service.GeneratorService;
import com.example.service.TableFieldService;
import com.example.service.TableInfoService;
import com.example.utils.ConvertUtils;
import com.example.utils.DbUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 代码生成
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@Tag(name = "代码生成")
public class GeneratorController {
    @Autowired
    private GeneratorService generatorService;
    @Autowired
    private TableInfoService tableInfoService;
    @Autowired
    private TableFieldService tableFieldService;
    @Autowired
    private DictFeignClient dictFeignClient;

    @GetMapping("table/page")
    @Operation(summary = "分页")
    public Result<PageData<GenTableInfoDTO>> pageTable(@RequestParam Map<String, Object> params) {
        PageData<GenTableInfoDTO> page = tableInfoService.page(params);

        return new Result<PageData<GenTableInfoDTO>>().ok(page);
    }

    @GetMapping("table/{id}")
    @Operation(summary = "详情")
    public Result<GenTableInfoDTO> getTable(@PathVariable("id") Long id) {
        TableInfoEntity table = tableInfoService.selectById(id);
        GenTableInfoDTO genTableInfoDTO = ConvertUtils.sourceToTarget(table, GenTableInfoDTO.class);

        List<GenTableFieldDTO> fieldList = tableFieldService.getByTableName(table.getTableName());
        genTableInfoDTO.setFields(fieldList);

        return new Result<GenTableInfoDTO>().ok(genTableInfoDTO);
    }

    @PutMapping("table")
    @Operation(summary = "修改")
    public Result<String> updateTable(@RequestBody TableInfoEntity tableInfo) {
        tableInfoService.updateById(tableInfo);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping("table")
    @Operation(summary = "删除")
    public Result<String> deleteTable(@RequestBody Long[] ids) {
        tableInfoService.deleteBatchIds(ids);

        return new Result<String>().ok("ok");
    }

    /**
     * 获取数据源中所有表
     */
    @GetMapping("datasource/table/list/{id}")
    @Operation(summary = "列表")
    public Result<List<TableInfoEntity>> getDataSourceTableList(@PathVariable("id") Long id) {
        try {
            //初始化配置信息
            DataSourceInfo info = generatorService.getDataSourceInfo(id);
            List<TableInfoEntity> tableInfoList = DbUtils.getTablesInfoList(info);

            return new Result<List<TableInfoEntity>>().ok(tableInfoList);
        } catch (Exception e) {
            return new Result<List<TableInfoEntity>>().error("数据源配置错误，请检查数据源配置！" + e.getMessage());
        }
    }

    /**
     * 导入数据源中的表
     */
    @PostMapping("datasource/table")
    @Operation(summary = "导入")
    public Result<String> datasourceTable(@RequestBody GenTableInfoDTO genTableInfoDTO) {
        ValidatorUtils.validateEntity(genTableInfoDTO);
        TableInfoEntity tableInfoEntity = ConvertUtils.sourceToTarget(genTableInfoDTO, TableInfoEntity.class);
        generatorService.datasourceTable(tableInfoEntity);

        return new Result<String>().ok("ok");
    }

    /**
     * 更新列数据
     */
    @PutMapping("table/field/{tableId}")
    @Operation(summary = "更新")
    public Result<String> updateTableField(@PathVariable("tableId") Long tableId, @RequestBody List<GenTableFieldDTO> tableFieldList) {
        List<TableFieldEntity> tableFieldEntities = ConvertUtils.sourceToTarget(tableFieldList, TableFieldEntity.class);
        generatorService.updateTableField(tableId, tableFieldEntities);

        return new Result<String>().ok("ok");
    }

    @GetMapping("dict")
    @Operation(summary = "字典")
    public Result<List<SysDictTypeDTO>> dict() {
        return dictFeignClient.getDictTypeList();
    }

    /**
     * 生成代码
     */
    @PostMapping("generator")
    @Operation(summary = "生成代码")
    public Result<String> generator(@RequestBody GenTableInfoDTO genTableInfoDTO) {
        //保存表信息
        tableInfoService.updateById(ConvertUtils.sourceToTarget(genTableInfoDTO, TableInfoEntity.class));

        //生成代码
        generatorService.generatorCode(genTableInfoDTO);

        return new Result<String>().ok("ok");
    }

    /**
     * 创建菜单
     */
    @PostMapping("menu")
    @Operation(summary = "创建菜单")
    public Result<String> menu(@RequestBody MenuEntity menu) {
        //创建菜单
        generatorService.generatorMenu(menu);

        return new Result<String>().ok("ok");
    }
}